package com.ruoyi.listener.listener;

import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.ruoyi.listener.entity.AliyunSmsSend;
//import com.ruoyi.listener.feign.MailClient;
import com.ruoyi.listener.feign.MailClient;
import com.ruoyi.listener.utils.DingDingUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * @author Mr.Wang
 * @date 23-8-13
 */
@Component
public class SpringRabbitListener {
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private AliyunSmsSend aliyunSmsSend;
    @Autowired
    private DingDingUtils dingDingUtils;
    @Autowired
    private MailClient mailClient;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queueEmail"),
            exchange = @Exchange(name = "tledu.direct", type = ExchangeTypes.FANOUT)
    ))
    public void listenDirectQueueEmail(String msg){
            System.out.println("消费者接收到direct.queueEmail的消息：【" + msg + "】");
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("2220867980@qq.com");
            message.setTo("3213387388@qq.com");
            message.setSubject("协同水提醒");
            message.setText(msg);
            mailSender.send(message);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queueSms"),
            exchange = @Exchange(name = "tledu.direct", type = ExchangeTypes.FANOUT)
    ))
    public void listenDirectQueueSms(String msg){
        System.out.println("消费者接收到direct.queueSms的消息：【" + msg + "】");
        System.out.println(aliyunSmsSend);
            // 替换为实际的手机号码、签名名称、模板代码和模板参数
            String phoneNumber = "15930971757";
            String signName = "协同水提醒";
            String templateCode = "SMS_476705008";  // 确保模板代码正确
        // 使用模板字符串，其中 {notice} 是占位符
        String templateParam = "{\"notice\":\"{notice}\"}";

        // 使用 String.replace 方法将 msg 插入到模板字符串中
        String formattedTemplateParam = templateParam.replace("{notice}", msg);
        try {
            SendSmsResponse response = aliyunSmsSend.sendSms(phoneNumber, signName, templateCode, formattedTemplateParam);
//            System.out.println(JSON.toJSONString(response));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queueRobot"),
            exchange = @Exchange(name = "tledu.direct", type = ExchangeTypes.FANOUT)
    ))
    public void listenDirectQueueRobot(String msg){
        System.out.println("消费者接收到direct.queueRobot的消息：【" + msg + "】") ;
        String url ="https://oapi.dingtalk.com/robot/send?access_token=503a4704c177693b44cb570d7fe6f744a3e5ca4408c980097cbd14a1e59b6ff5";
        dingDingUtils.send(url,msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queueMail"),
            exchange = @Exchange(name = "tledu.direct", type = ExchangeTypes.FANOUT)
    ))
    public void listenDirectQueueMail(String msg){
        System.out.println("消费者接收到direct.queueMail的消息：【" + msg + "】") ;
        mailClient.sendMail(msg);
    }
}
